/*
 * TableModelZonas.java
 *
 * Created on 4 de febrero de 2007, 11:59 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.util;

import ig.metrica.riesgo.BandasDTO;
import ig.metrica.riesgo.FactorRiesgoMatrizCorrelacionMetrica;
import ig.metrica.riesgo.ZonaDTO;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author jsanchez
 */
public class TableModelZonas extends AbstractTableModel {
//    private Collection<ZonaDTO> collection=null;
    private boolean[]   editables={true,true};
    private String[] columnNames = {
        "Zona", "Ajuste Horizontal Por Zona"
    };
    private Hashtable<Integer,ZonaDTO> hashTable=new Hashtable<Integer,ZonaDTO>();
    private ZonaDTO tmp=null;
    private Integer i=0;
    /** Creates a new instance of TableModelZonas */
    public TableModelZonas(Collection<ZonaDTO> collection) {
        for (ZonaDTO elem : collection) {
            hashTable.put(i,elem);
            i++;
        }
    }
    
    public void setZonas(Collection<ZonaDTO> c) {
        i=0;
        for (ZonaDTO z : c) {
            System.out.println("-------------------------\nZonas:"+z);
            for (BandasDTO b: z.getBandas()) {
                System.out.println("Banda:"+b.getNumero());
                for (FactorRiesgoMatrizCorrelacionMetrica f:b.getCollection()) {
                    System.out.println(f.getNombre());
                    ig.metrica.riesgo.CambioEsperadoPBS pbs=f.getCambioEsperadoPBS();
                    System.out.println(pbs.getLimiteInferior());
                    System.out.println(pbs.getLimiteSuperior());
                    System.out.println(pbs.getCambioEsperado());
                }
            }
        }
        for (ZonaDTO elem : c) {
            hashTable.put(i,elem);
            i++;
        }
        fireTableDataChanged();
    }
    
    public String getColumnName(int col) {
        return columnNames[col];
    }
    public int getRowCount() {
        return hashTable.size();
    }
    
    public int getColumnCount() {
        return columnNames.length;
    }
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    public boolean isCellEditable(int row, int col) {
        return (editables[col]==true) ? true : false;
    }
    
    public synchronized void newRow(Integer idMatriz){
        Integer x=0;
        boolean salir=true;
        
        while(salir){
            salir=false;
            x++;
            Enumeration<ZonaDTO> enumm=hashTable.elements();
            while (enumm.hasMoreElements()) {                
                ZonaDTO zona=enumm.nextElement();
                if(zona.getNumero().equals(x)){
                    salir=true;
                    break;
                }
            }
        }
        
        ZonaDTO z=new ZonaDTO(KeyGenerator.getInstance().getNewKey(),idMatriz,x,0.0,"Ups");
        z.setNuevo(true);
        hashTable.put(i++,z);
        fireTableDataChanged();
    }
    
    public void removeRow(Integer row){
        hashTable.remove(row);
        ordenar();
        fireTableDataChanged();
    }
    private void ordenar(){
        Hashtable<Integer,ZonaDTO> clon=(Hashtable)hashTable.clone();
        Enumeration<ZonaDTO> enumm=clon.elements();
        hashTable.clear();
        Integer ante=clon.size();
        i=0;
        while(enumm.hasMoreElements()){
            ZonaDTO z=enumm.nextElement();
            hashTable.put(i,z);
            i++;
        }
        clon=(Hashtable)hashTable.clone();
        enumm=clon.elements();        
        Integer j=0;
        hashTable.clear();
        while(enumm.hasMoreElements()){
            ZonaDTO z=enumm.nextElement();
            hashTable.put(j,z);
            j++;
        }        
    }
    public Object getValueAt(int rowIndex, int columnIndex) {
//        System.out.println(rowIndex+","+columnIndex);
        this.tmp=this.hashTable.get(rowIndex);
        if(columnIndex==0)
            return this.tmp.getNumero();
        else if(columnIndex==1)
            return this.tmp.getAjusteHorizontal();
        return null;
    }
    
    public void setValueAt(Object value, int row, int col){
        this.tmp=this.hashTable.get(row);
        if(col==0)
            this.tmp.setNumero(Integer.valueOf(value.toString()));
        else if(col==1)
            this.tmp.setAjusteHorizontal(Double.valueOf(value.toString()));
        this.fireTableCellUpdated(row,col);
    }
    public Collection<ZonaDTO> getData(){
        Collection<ZonaDTO> c=new ArrayList<ZonaDTO>();
        Enumeration<ZonaDTO> enumm=hashTable.elements();
        while (enumm.hasMoreElements()) {
            c.add(enumm.nextElement());
        }
        return c;
    }
    public ZonaDTO getDTO(int rowIndex){
        return this.hashTable.get(rowIndex);
    }
}